package web.admin.shop.product;


import domain.shop.Product;
import org.apache.wicket.Component;
import org.apache.wicket.extensions.ajax.markup.html.AjaxLazyLoadPanel;
import org.apache.wicket.extensions.ajax.markup.html.tabs.AjaxTabbedPanel;
import org.apache.wicket.extensions.markup.html.tabs.AbstractTab;
import org.apache.wicket.extensions.markup.html.tabs.ITab;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import service.facade.admin.ShopAdminService;
import web.admin.components.TinyMceBehavior;
import web.admin.shop.product.tabs.*;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Provides settings of particular product
 */
public final class ProductPanel extends Panel {

    @SpringBean
    private ShopAdminService shopAdminService;
    private final IModel<Product> productModel;
    private static Logger logger = Logger.getLogger(ProductPanel.class.getName());

    public ProductPanel(String id, final Long productId, final ProductAdminPanel parent) {
        super(id);
        productModel = new LoadableDetachableModel<Product>() {

            @Override
            protected Product load() {
                return shopAdminService.findProductById(productId);
            }
        };
        // create a list of ITab objects used to feed the tabbed panel
        List<ITab> tabs = new ArrayList<ITab>();
        tabs.add(new AbstractTab(new Model<String>(getLocalizer().getString("product.tab.main", this))) {
            @Override
            public Panel getPanel(String panelId) {
                return new AjaxLazyLoadPanel(panelId) {
                    @Override
                    public Component getLazyLoadComponent(String id) {
                        return new Edit(id, parent);
                    }
                };
            }
        });
        tabs.add(new AbstractTab(new Model<String>(getLocalizer().getString("product.tab.immages", this))) {
            @Override
            public Panel getPanel(String panelId) {
                return new AjaxLazyLoadPanel(panelId) {
                    @Override
                    public Component getLazyLoadComponent(String id) {
                        return new UploadPanel(id, productId);
                    }
                };
            }
        });

        tabs.add(new AbstractTab(new Model<String>(getLocalizer().getString("product.tab.components", this))) {
            @Override
            public Panel getPanel(String panelId) {
                return new AjaxLazyLoadPanel(panelId) {
                    @Override
                    public Component getLazyLoadComponent(String id) {
                        return new ComponentProductPanel(id, productId);
                    }
                };
            }
        });

        tabs.add(new AbstractTab(new Model<String>(getLocalizer().getString("product.tab.similarProducts", this))) {
            @Override
            public Panel getPanel(String panelId) {
                return new AjaxLazyLoadPanel(panelId) {
                    @Override
                    public Component getLazyLoadComponent(String id) {
                        return new RelatedProductsPanel(id, productId);
                    }
                };
            }
        });
        tabs.add(new AbstractTab(new Model<String>(getLocalizer().getString("product.tab.categories", this))) {
            @Override
            public Panel getPanel(String panelId) {
                return new AjaxLazyLoadPanel(panelId) {
                    @Override
                    public Component getLazyLoadComponent(String id) {
                        return new ProductCategoriesPanel(id, productId);
                    }
                };
            }
        });
        tabs.add(new AbstractTab(new Model<String>(getLocalizer().getString("product.tab.atributes", this))) {
            @Override
            public Panel getPanel(String panelId) {
                return new AjaxLazyLoadPanel(panelId) {
                    @Override
                    public Component getLazyLoadComponent(String id) {
                        return new ProductAttributesPanel(id, productModel);
                    }
                };
            }
        });

        final AjaxTabbedPanel tabbedPanel = new AjaxTabbedPanel("tabs", tabs);
        add(tabbedPanel);
        tabbedPanel.setOutputMarkupId(true);
        setOutputMarkupId(true);

    }

    private class Edit extends Panel {
        public Edit(String id, final ProductAdminPanel parent) {
            super(id);

            Form form = new Form("form") {

                {
                    final Button delete = new Button("delete") {


                        @Override
                        public void onSubmit() {
                            try {
                                parent.detachProductsModel();
                                shopAdminService.deleteProduct(productModel.getObject());
                                getSession().info(getLocalizer().getString("deleted", this));
                                parent.replacePanelToEmpty();

                            } catch (Exception e) {
                                logger.log(Level.SEVERE, e.getMessage(), e);
                                error(getLocalizer().getString("system.error", this));
                            }
                        }
                    };
                    add(delete);
                    delete.setDefaultFormProcessing(false);

                    add(new Button("save") {

                        @Override
                        public void onSubmit() {
                            try {
                                shopAdminService.updateProduct(productModel.getObject());
                                info(getLocalizer().getString("saved", this));
                            } catch (Exception e) {
                                logger.log(Level.SEVERE, null, e);
                                error(getLocalizer().getString("system.error", this));
                            }
                        }
                    });
                }
            };


            form.add(new TextField("title", new PropertyModel(productModel, "title")).setRequired(true));
            form.add(new TextField("price", new PropertyModel(productModel, "price")).setRequired(true));
            form.add(new TextField("carType", new PropertyModel(productModel, "carType")));
            form.add(new TextField("description", new PropertyModel(productModel, "description")));
            form.add(new TextField("keywords", new PropertyModel(productModel, "keywords")));
            TextArea shortContent = new TextArea("shortContent", new PropertyModel(productModel, "shortContent"));
            form.add(shortContent);
            shortContent.add(new TinyMceBehavior(true));
            TextArea content = new TextArea("content", new PropertyModel(productModel, "content"));
            form.add(content);
            content.add(new TinyMceBehavior(true));

            add(form);
            form.setOutputMarkupId(true);
            setOutputMarkupId(true);

        }
    }
}
